<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/reset.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/baoyang.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/css/patrol/index.css">
    <link rel="stylesheet" href="{{ layui.setter.base }}style/page.css">
</script>
<style scoped>
    .box {
        padding: 28px 27px 30px 30px;
    }
    .layui-form-item{margin-top: 15px;}
    .btn1{
      width: 60px;
      font-size: 11px;
      margin: 0;
      padding: 0 5px;
      border: 0;
      text-align: center;
      background-color: #FF4C4C;
      margin-top: 15px;
      line-height: 22px;
      height: 22px;
    }
</style>
<!-- 新增项目和自定义按钮 -->
<div class="box layui-fluid layui-row">
    <div class="BaoYangButton layui-col-xs12 layui-col-sm12 layui-col-md12">
        <button type="button" class="add-project layui-btn layui-btn-primary" data-type="addproduct">
            <i class="layui-icon layui-icon-add-circle" style="font-size:15px;color:#1E9FFF;"></i>
            <span>新增项目</span>
        </button>
    </div>
    <!--  -->
    <div class="layui-col-xs12 layui-col-sm12 layui-col-md12">
        <form class="layui-form formData_llp" lay-filter="app-content-comment">
            <div class="layui-form-item itemOfone">
                <div class="firestBreak layui-col-md2 layui-col-xs2 layui-col-sm2">
                    <div class="layui-inline">
                        <span class="sbbh labelinp" och="DEVNumber">保养项目名称：</span>
                        <input id="Number" name="" class="inp baoYang1" lay-ignore autocomplete="off"
                            style="border: 1px solid #e6e6e6;height: 22px;width: 70px;">
                        </input>
                    </div>
                </div>

                <div class="layui-col-md2 layui-col-xs2 layui-col-sm2">
                    <div class="layui-inline">
                        <span class="sbbh labelinp" och="DEVNumber">保养部位：</span>
                        <input id="Number" name="" class="inp baoYang2" lay-ignore autocomplete="off"
                            style="border: 1px solid #e6e6e6;height: 22px;width: 70px;">
                        </input>
                    </div>
                </div>

                <div class="layui-col-md2 layui-col-xs2 layui-col-sm2">
                    <div class="layui-inline">
                        <span class="sbbh labelinp" och="DEVNumber">保养说明:</span>
                        <input id="Number" name="" class="inp baoYang3" lay-ignore autocomplete="off"
                            style="border: 1px solid #e6e6e6;height: 22px;width: 70px;">
                        </input>
                    </div>
                </div>

                <div class="layui-col-md2 layui-col-xs2 layui-col-sm2" style="max-height: 130px;overflow: auto;">
                    <div class="layui-inline">
                        <span class="sbbh labelinp" och="DEVNumber">相关文档:</span>
                        <input id="Number" name="" class="inp baoYang4" lay-ignore autocomplete="off"
                            style="border: 1px solid #e6e6e6;height: 22px;width: 70px;">
                        </input>
                    </div>
                </div>

                <div class="layui-col-md2 layui-col-xs2 layui-col-sm2 ">
                    <button type="button" data-lang="Inquire" class="ChaXunButton labelbtn" data_cn="高级搜索"
                        data_en="advanced search" lay-submit lay-filter="query" id="queryButton"
                        style="background:#4C96CF;">查询</button>
                </div>
            </div>
        </form>

        <!-- 数据表格 -->
        <table class="BaoYangTable layui-table" id="maintainFormData" lay-filter="maintainFormData" style="display: flex;"></table>
        <!-- 删除 -->
        <button type="button" class="layui-btn layui-btn-xs layui-btn-radius btn1"  data-type="deletePro">
           <i class="layui-icon layui-icon-delete"></i>
           删除
        </button>
    </div>
</div>
<script type="text/html" id="baoYangXiang">

    <i class="layui-icon layui-icon-edit" style="margin-right:10px" lay-event="edit"></i>
    <!-- 删除 -->
    <i class="layui-icon layui-icon-delete" lay-event="deleteElement"></i>
    {{#  if(d.tcMaintIsDel=="0"){ }}
     
    <i class="layui-icon layui-icon-pause" style="padding-left: 10px;cursor:pointer;font-size:13px;" lay-event="toogele"></i>
    {{#  } else  if(d.tcMaintIsDel=="1"){ }}

    <i class="layui-icon layui-icon-play" style="padding-left: 10px;font-size:13px;"   lay-event="toogele1"></i>

    {{#  } else  if(d.state=="reject"){ }}
    <span>-</span>

    {{#  } else  { }}

    {{#  } }}
</script>
<script type="text/html" id="project">

</script>
<script>
    layui.use(['admin', 'form', 'user', 'table', 'api', 'laytpl', 'utill', 'laydate','treeSelect'], function () {
        var $ = layui.$,
            admin = layui.admin,
            view = layui.view,
            api = layui.api,
            table = layui.table,
            setter = layui.setter,
            laytpl = layui.laytpl,
            utill = layui.utill,
            laydate = layui.laydate,
            form = layui.form;
            treeSelect = layui.treeSelect;
            var token = JSON.parse(sessionStorage.getItem('ipay-merchant')).token;
        var _TPL = {
            tcDocName: function (d) {
                if (d.tcDocName != null) {
                    return `<div class="DocName" data-name="${d.tcMaintDocid}" >${d.tcDocName}</div>`
                } else {
                    return `<div></div>`
                }
            },
             tcMaintIsDel:function(d){
            if (d.tcMaintIsDel == 0) {
                return "<span>启用</span>";
            } else if (d.tcMaintIsDel == 1) {
                return "<span>禁用</span>";
              } 
            }
        }
        // 列定制
        utill.columnCustom('#maintainFormData', "mtProjectVO", {
            // 自己表格的接口
            url: api.host + api.selectAllMaintenanceList,
            where: {
                tcMaintName: "",
                tcMaintPart: "",
                tcMaintDsc: "",
                tcDocName: ""
            },
            toolbar: {
                toolbar: '#baoYangXiang',
                title: '操作',
                event: 'loading',
                align: 'center'
            },
            templet: _TPL,
            checkbox: true
        });
        rander();
        function rander() {
            $('.box').on('click', '.DocName', function () {
                var val = $(this).text();
                var tcDocId = $(this).data('name');
                if (tcDocId != '' && tcDocId != null) {
                    admin.req({
                        url: api.host + api.selectdetails_llp,
                        type: 'get',
                        dataType: 'json',
                        contentType: 'application/json',
                        data: { tcDocId },
                        success: function (data) {
                            var data = data.data.attachlist;
                            if (data == '' || data == null) {
                                layer.msg('无附件!')
                            } else {
                                var html = '';
                                data.forEach(elem => {
                                    html += `<div ><a href="` + api.imghost + "/" + `${elem.tcFileUrl}?token=${token}" target="blank">${elem.tcFileName}</a></div>`;
                                    layer.open({
                                        type: 0,
                                        title: '文档',
                                        area: ['50', '50'],
                                        content: html
                                    });
                                })
                            }
                        }
                    })
                } else {
                    layer.msg('无附件!')
                }
            })
            // 保养项目高查
            $("#queryButton").click(function () {
                var baoYang1 = $(".baoYang1").val();
                var baoYang2 = $(".baoYang2").val();
                var baoYang3 = $(".baoYang3").val();
                var baoYang4 = $(".baoYang4").val();
                table.reload('#maintainFormData', {
                    where: {
                        tcMaintName: baoYang1,    //项目名称
                        tcMaintPart: baoYang2,    //项目部位
                        tcMaintDsc: baoYang3,     //保养说明
                        tcDocName: baoYang4       //相关文档
                    },
                    page: {
                        curr: 1
                    }
                })
            })
            table.on('tool(maintainFormData)', function (obj) {
                var data = obj.data
                if (obj.event === 'edit') {
                    admin.popup({
                        title: '编辑',
                        area: ['380px', '450px'],
                        id: 'LAY-popup-user-add',
                        success: function (layero, index) {
                            view(this.id).render('maintain/alert/Edit_alert', data).done(function () {
                                var tcCatgName = data.tcCatgName;
                                $("#tcCatgId").val(tcCatgName);
                                function wd(data){
                                    if(data){
                                        $('#treeSelect').empty()
                                        $('#treeSelect').removeAttr('disabled');
                                        admin.req({
                                            url: api.host + api.sbwd.doSelectList,
                                            type: 'post',
                                            contentType: 'application/json',
                                            data:JSON.stringify({
                                                tcCatgId: data,
                                                pageNum: 1,
                                                pageSize: 10000
                                            }),
                                            done: function (res) {
                                                var str = '<option value=""></option>'
                                                for (let index = 0; index < res.data.list.length; index++) {
                                                    if(res.data.list[index] == null){
                                                        continue
                                                    }else{
                                                          str = str + `<option value="${res.data.list[index].tcDocId}">${res.data.list[index].tcDocName}</option>`
                                                    }            
                                                }
                                                $('#treeSelect').html(str);
                                                form.render();
                                            }
                                        });
                                    }
                                }
                                admin.req({
                                    url: api.host + api.selectoption,
                                    done: function (res) {
                                        function transferKey(odata) {
                                            odata.forEach(obj => {
                                                obj.title = obj.tcCatgName
                                                obj.children = obj.childList
                                                delete obj["tcCatgName"]
                                                delete obj["childList"]
                                                if (obj.children instanceof Array) {
                                                    transferKey(obj.children)
                                                }
                                            })
                                            return odata
                                        }
                                        odataArr = transferKey(res.data)
                                        treeSelect.render({
                                            elem: "#tcCatgId",
                                            node: odataArr,
                                            done: (data) => {
                                                $("#tcCatgId").val(data.data.title);
                                                wd(data.data.tcCatgId);
                                            }
                                        })
                                    }
                                });
                                // 取消按钮点击事件
                                $(".btn2").click(function () {
                                    layer.close(index);
                                })
                                // 再一次渲染表格
                                form.render(null, 'layuiadmin-form-project');
                                $('#treeSelect').find('option').text(data.tcDocName)
                                $('#treeSelect').find('option').val(data.tcMaintDocid)
                                // 回显效果
                                form.val("layuiadmin-form-project", {
                                    "tcMaintName": data.tcMaintName,
                                    "tcMaintPart": data.tcMaintPart,
                                    "tcMaintDsc": data.tcMaintDsc,
                                    "tcDocName": data.tcDocName
                                })
                                form.render()
                                //监听保养项目修改
                                form.on('submit(layuiadmin-app-form-submit)', function (data) {
                                    // 把数据传递到后台
                                    var field = data.field; //获取提交的字段
                                    field.tcMaintId = obj.data.tcMaintId
                                    field.tcMaintDocid = field.tcDocName
                                    field = JSON.stringify(field);
                                    admin.req({
                                        url: api.host + api.updateSelectByKey,
                                        data: field,
                                        type: "POST",
                                        dataType: 'json',
                                        contentType: 'application/json',
                                        done: function (res) {
                                            layui.table.reload('#maintainFormData'); //重载表格  
                                            layer.closeAll(); //执行关闭 
                                            layer.msg(res.message);
                                        }
                                    });
                                });
                            });
                        }
                    });
                }
                // （ok）
                else if (obj.event === 'delete') {
                    var arr = new Array();
                    arr.push(data.tcMaintId);
                    layer.confirm('真的删除吗？', function (index) {
                        admin.req({
                            url: api.host + api.deleteMaintenance,
                            type: 'post',
                            contentType: 'application/json',
                            dataType: 'json',
                            data: JSON.stringify(arr),
                            //向服务端发送删除的id
                            done: function (res) {
                                layui.table.reload('#maintainFormData'); //重载表格  
                                layer.close(index); //执行关闭 
                                layer.msg(res.message);
                            }
                        })
                    })
                } else if (obj.event === "toogele") {
                    let id = data.tcMaintId;
                    admin.req({
                        type: 'get',
                        dataType: 'json',
                        contentType: 'application/json',
                        url: api.host + api.deleteMaintenance,
                        data: { id },
                        done: function (res) {
                            layui.table.reload('#maintainFormData'); //重载表格  
                            layer.msg(res.message);
                        }
                    });
                } else if (obj.event === "toogele1") {
                    let id = data.tcMaintId;
                    admin.req({
                        type: 'get',
                        dataType: 'json',
                        contentType: 'application/json',
                        url: api.host + api.deleteMaintenance,
                        data: { id },
                        done: function (res) {
                            layui.table.reload('#maintainFormData'); //重载表格  
                            layer.msg(res.message);
                        }
                    });
                }else if(obj.event === "deleteElement"){
                   var id_llp = obj.data.tcMaintId;
                   var _Array = [];
                   _Array.push(id_llp);
                   admin.req({
                        type: 'post',
                        dataType: 'json',
                        contentType: 'application/json',
                        url: api.host + api.deleteMaint_llp,
                        data: JSON.stringify(_Array),
                        done: function (res) {
                            layui.table.reload('#maintainFormData'); //重载表格  
                            layer.msg(res.message);
                        }
                    });
                }
            });

            //事件（ok）
            var active = {
                // 新建项目的弹出层的
                addproduct: function () {
                    admin.popup({
                        title: '新增项目',
                        area: ['380px', '450px'],
                        shadeClose: true,
                        id: 'LAY-popup-user-add',
                        success: function (layero, index) {
                            view(this.id).render('maintain/alert/addProject').done(function () {
                                // 取消按钮
                                $(".btn2").click(function () {
                                    layer.close(index);
                                })
                                //监听提交
                                form.on('submit(layuiadmin-app-form-submit)', function (
                                    data) {
                                    var one_llp = $("#one_llp").val();
                                    var two_llp = $("#two_llp").val();
                                    var three_llp = $("#three_llp").val();
                                    var five_llp = $("#MainDocId").val();
                                    if (one_llp != "" && two_llp != "" && three_llp != "" && five_llp != "") {
                                        var field = data.field; //获取提交的字段
                                        var fieldOne = JSON.stringify(field);
                                        admin.req({
                                            url: api.host + api.insertSelective,
                                            data: fieldOne,
                                            type: "POST",
                                            dataType: 'json',
                                            contentType: 'application/json',
                                            done: function (res) {
                                                layui.table.reload('#maintainFormData'); //重载表格  
                                                layer.close(index); //执行关闭 
                                                layer.msg(res.message);
                                            }
                                        });
                                    } else {
                                        layer.msg('请填写完整');
                                    }
                                });
                                // 取消添加事件
                                $('.layui-form-item').on('click', '.btn2', function () {
                                    layer.close(index);
                                });
                            });
                        }
                    });
                },
                deletePro:function(){
                    var checkStatus = table.checkStatus('#maintainFormData');
                    if (checkStatus.data.length == 0) {
                       parent.layer.msg('请先选择要删除的数据行！', { icon: 2 });
                    return;
                    }
                   var _Array_llp = [];
                   for (var i = 0; i < checkStatus.data.length; i++) {
                     _Array_llp.push(checkStatus.data[i].tcMaintId);
                  }
                   admin.req({
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json',
                    url: api.host + api.deleteMaint_llp,
                    data: JSON.stringify(_Array_llp),
                    success: function (data) {
                        layer.msg(data.message);
                        layui.table.reload('#maintainFormData'); //重载表格  
                    }
                  });
                }
            };

            $('#delete').click(function () {
                layui.table.reload('maintainFormData'); //重载表格  
            })
            $('.layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        }
    });
</script>

</html>